package com.example.jichu_1;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.jichu_1.fragment.HomeFragment;
import com.example.jichu_1.fragment.LoveFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private ViewPager mVp;
    private TabLayout mTablayout;
    private LinearLayout mLl;
    private NavigationView mNav;
    private DrawerLayout mDrl;
    private ActionBarDrawerToggle drawerToggle;
    private ImageView headeriv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        initToolbar();
        initNav();
    }

    private void initNav() {
        View headerView = mNav.getHeaderView(0);
        mNav.setItemIconTintList(null);
        headeriv = headerView.findViewById(R.id.header_iv);
        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 1);
            }
        });
        mNav.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.setting:
                        Toast.makeText(HomeActivity.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.love:
                        Toast.makeText(HomeActivity.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.music:
                        Toast.makeText(HomeActivity.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                        break;
                }
                mDrl.closeDrawer(Gravity.LEFT);
                return false;
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK) {
            Uri data1 = data.getData();
            Glide.with(this).load(data1).apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(headeriv);
        }
    }

    private void initToolbar() {
        mToolbar.setTitle("主标题");
        mToolbar.setSubtitle("副标题");
        setSupportActionBar(mToolbar);
        drawerToggle = new ActionBarDrawerToggle(this, mDrl, mToolbar, R.string.app_name, R.string.app_name);
        drawerToggle.syncState();
        mDrl.addDrawerListener(drawerToggle);
        mDrl.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                mLl.setX(drawerView.getWidth() * slideOffset);
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });
    }



    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mVp = (ViewPager) findViewById(R.id.vp);
        mTablayout = (TabLayout) findViewById(R.id.tablayout);
        mLl = (LinearLayout) findViewById(R.id.ll);
        mNav = (NavigationView) findViewById(R.id.nav);
        mDrl = (DrawerLayout) findViewById(R.id.drl);
        final ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new LoveFragment());
        final ArrayList<String> strings = new ArrayList<>();
        strings.add("首页");
        strings.add("收藏");
        mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return strings.get(position);
            }
        });
        mTablayout.setupWithViewPager(mVp);
    }

}
